package com.jf.exam.controller.manage;

import com.jf.exam.pojo.vo.StudentVO;
import com.jf.exam.service.StudentService;
import com.jf.exam.utils.PageBean;
import com.jf.exam.utils.Result;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;

/**
 * 学生管理
 */
@RequestMapping("/admin/student")
@Controller
public class ManageStudentController {
    //学生Service
    @Autowired
    StudentService studentService;

    @Value("#{properties['student.pageSize']}")
    private int pageSize;

    @Value("#{properties['student.navigatePages']}")
    private int navigatePages;


    /**
     * 学生列表
     */
    @RequestMapping("/list")
    public String list(StudentVO query, Model model) throws Exception {
        model.addAttribute("function", 5);
        query.setPageSize(pageSize);
        query.setSize(navigatePages);
        PageBean pageBean = studentService.listStudent(query);
        model.addAttribute("pageBean", pageBean);
        model.addAttribute("query", query);
        return "admin/student_list";
    }

    /**
     * 检查学生是否存在
     *
     * @param student 学生id
     * @return
     */
    @RequestMapping("/check")
    @ResponseBody
    public Result check(StudentVO student) {
        return studentService.isExist(student);
    }

    //add?id=1111&clazz=1&name=%E8%B5%B5%E5%85%AD
    @RequestMapping("/add")
    @ResponseBody
    public Result add(StudentVO student, Integer clazz) throws Exception {
        student.setId(UUID.randomUUID().toString());
        student.setModified(0);
        student.setDelFlag("0");
        student.setPassword(new Md5Hash("admin", student.getName()).toString());
        student.setFkClass(clazz);
        return studentService.addStudent(student);
    }

    /**
     * 学生修改
     *
     * @param clazz 班级id
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Result edit(StudentVO student, Integer clazz) throws Exception {
        student.setFkClass(clazz);
        return studentService.updateStudent(student);
    }

    /**
     * 通过id删除学生信息
     *
     * @param sid 学生id
     * @return 删除结果
     */
    @RequestMapping("/delete/{sid}")
    @ResponseBody
    public Result delete(@PathVariable String sid) throws Exception {
        StudentVO student = new StudentVO();
        student.setId(sid);
        return studentService.deleteStudent(student);
    }

}
